﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using posttrackerApi2.Models;
using posttrackerApi2.Models.TypesHelper;

namespace posttrackerApi2.Controllers
{
    public class TrackerController : ApiController
    {
        private posttracker_db db = new posttracker_db();

        // GET api/Tracker
        public IEnumerable<TrackData> GetTrackDatas()
        {
            var trackdatas = db.TrackDatas.Include(t => t.TrackNumber).Include(t => t.Source);
            return trackdatas.AsEnumerable();
        }

        // GET api/Tracker/GetTrackData/5
        public TrackData GetTrackData(int id)
        {
            TrackData trackdata = db.TrackDatas.Find(id);
            if (trackdata == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return trackdata;
        }

        

        // PUT api/Tracker/PutTrackData/5
        public HttpResponseMessage PutTrackData(int id, TrackData trackdata)
        {
            if (ModelState.IsValid && id == trackdata.Id)
            {
                db.Entry(trackdata).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Tracker
        public HttpResponseMessage PostTrackData(TrackData trackdata)
        {
            if (ModelState.IsValid)
            {
                db.TrackDatas.Add(trackdata);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, trackdata);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = trackdata.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Tracker/5
        public HttpResponseMessage DeleteTrackData(int id)
        {
            TrackData trackdata = db.TrackDatas.Find(id);
            if (trackdata == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.TrackDatas.Remove(trackdata);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, trackdata);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}